package com.lfg.leetcode.leetcode100;


import java.util.ArrayList;
import java.util.List;

public class No131partition {

    public static List<List<String>> partition(String s) {
        if (null == s) {
            return new ArrayList<>(0);
        }
        int length = s.length();
        List<List<String>> ret = new ArrayList<>(length);
        partition(s, 0, ret, new ArrayList<String>(length));
        return ret;
    }

    public static void partition(String s, int start, List<List<String>> ret, List<String> data) {
        if (start >= s.length()) {
            ret.add(new ArrayList<>(data));
            return;
        }

        for (int i = start; i < s.length(); i++) {
            if (isPalindrome(s, start, i)) {
                data.add(s.substring(start, i + 1));
                partition(s, i + 1, ret, data);
                data.remove(data.size() - 1);
            }
        }

    }

    public static boolean isPalindrome(String s, int start, int end) {
        if (start > end) {
            return true;
        }
        while (start <= end) {
            if (s.charAt(start) != s.charAt(end)) {
                return false;
            }
            start++;
            end--;
        }
        return true;
    }
}
